<template>
  <div class="app-container-custom">
    <el-row :gutter="10">
      <fold :span="5" :xs="24" :show.sync='sideshow'>
        <el-card shadow="never">
          <org-tree ref="orgTree" modelType="'dept','community','park','carp'" @nodeClick="handleNodeClick"
                    @firstNodeClick="handleNodeClick"/>
        </el-card>
      </fold>
      <el-col :span="sideshow ? 19 : 24" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAddParking"  v-hasPermi="['property:parking:add']">新增车场</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAddCarport" v-hasPermi="['property:carport:add']">新增车位</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain size="mini" @click="handleDelete" v-hasPermi="['property:carport:remove']">删除</el-button>
          </el-col>
        </el-row>
        <el-card shadow="never">
          <community-form res="communityRefs" v-if="currentNode.modelType == 'community'" :currentNode="currentNode"/>
          <parking-main res="parkingRefs" v-if="currentNode.modelType == 'park'" :currentNode="currentNode"/>
          <carport-main res="carportRefs" v-if="currentNode.modelType == 'carp'" :currentNode="currentNode"/>
        </el-card>
      </el-col>
    </el-row>
    <parking-add-form ref="addParkingRefs" @ok="$refs.orgTree.refresh()"/>
    <carport-add-form ref="addCarportRefs" @ok="$refs.orgTree.refresh()"/>
  </div>
</template>

<script>

  import CommunityForm from "./community/form";
  import ParkingMain from "./parking/main";
  import CarportMain from "./carport/main";
  import ParkingAddForm from "./parking/addForm";
  import CarportAddForm from "./carport/addForm";
  import { delCarport } from "@/api/property/carport";
  import { delParking } from "@/api/property/parking";

  export default {
    name: "ParkingMange",
    components:{CommunityForm, ParkingMain,CarportMain,ParkingAddForm ,CarportAddForm },
    data() {
      return {
        currentNode:{},
        sideshow:true
      }
    },
    methods:{
      handleNodeClick(node){
        this.currentNode = node;
      },
      handleAddParking(){
        if(this.currentNode.modelType=='community'){
          this.$refs.addParkingRefs.show({currentNode:this.currentNode,type:'ADD'})
        }else{
          this.msgWarning("请选择所属项目")
        }
      },
      handleAddCarport(){
        if(this.currentNode.modelType=='park'){
          this.$refs.addCarportRefs.show({currentNode:this.currentNode,type:'ADD'})
        }else{
          this.msgWarning("请选择所属车场")
        }
      },
      handleDelete(){
        const ids = this.currentNode.id;
        if(this.currentNode.modelType=='park'){
          this.$confirm('是否确认删除车场为"' + this.currentNode.label + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delParking(ids);
          }).then(() => {
            this.$refs.orgTree.refresh()
            this.msgSuccess("删除车场成功");
          }).catch(() => {});
        }else if(this.currentNode.modelType=='carp'){
          this.$confirm('是否确认删除车位为"' + this.currentNode.label + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delCarport(ids);
          }).then(() => {
            this.$refs.orgTree.refresh()
            this.msgSuccess("删除车位成功");
          }).catch(() => {});
        }
      }
    }
  }
</script>

<style scoped>

</style>
